var app = getApp();
Page({
  data: {
	cdnUrl: '',
    isWxapp: true,
    userInfo: {
      id: 0,
      avatar: '/assets/images/avatar.png',
      nickname: '游客',
      balance: 0,
      score: 0,
      level: 0
    },
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#02B3BA',
		show: true,
		animated: false,
		back: false,
		title: "我的",
		/* 自定义tabbar */
		list: [
			{
					"pagePath": "/page/index/index",
					"iconPath": "/assets/images/tabbar/index.png",
					"selectedIconPath": "/assets/images/tabbar/index-hl.png",
					"text": "首页"
			},
			
			{
					"pagePath": "/page/discover/index",
					"iconPath": "/assets/images/tabbar/addon.png",
					"selectedIconPath": "/assets/images/tabbar/addon-hl.png",
					"text": "发现"
			},
			{
					"pagePath": "/page/my/index",
					"iconPath": "/assets/images/tabbar/my.png",
					"selectedIconPath": "/assets/images/tabbar/my-hl.png",
					"text": "我的"
			}
		],
		currentTab: 3
  },
  tabChange(e) {
	  this.setData({
	  	currentTab: e.detail.index
	  })
  	wx.reLaunch({
  	  url: e.detail.item.pagePath
  	})
  },
  onLoad: function () {
    var that = this;
    that.setData({
    	cdnUrl: app.cdnUrl,
    });
  },
  onShow: function () {
    var that = this;
    that.userInfo();
    if (app.globalData.userInfo) {
      that.setData({ 
        userInfo: app.globalData.userInfo,
      });
    }else{
			app.check(that);
		}
		if( that.data.userInfo.id == 0){
			that.userInfo();
		}
  },
  login: function () {
    var that = this;
    wx.getUserProfile({
      lang:'zh',
      desc:'授权用户信息',
      success:function(res){
        console.log(res)
        app.login(res.userInfo,function () {
          that.setData({ 
            userInfo: app.globalData.userInfo,
          });
        });
      },
      fail:function(e){
        app.info(JSON.stringify(e));
      }
    })
  },
	userInfo: function (){
		var that = this;
		app.request('/addons/cms/wxapp.user/userInfo', {}, function (data, ret) {
		  that.setData({
				userInfo: data.userInfo
		  });
		}, function (data, ret) {
		  // app.error(ret.msg);
		});
	}
  
})
